 
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <iostream>
#include <thread>
#include <list>
#include <iostream>
 
#define PORT 8606
#define IP "127.0.0.1"
 
int mSocket;
struct sockaddr_in servaddr;
socklen_t len;
std::list<int> connList;//用list来存放套接字，没有限制套接字的容量就可以实现一个server跟若干个client通信
 
 
void sendMsg(int sender,char* msg,int length)
{
    char buf[1024];
    memset(buf, 0 ,sizeof(buf));
    sprintf(buf,"玩家%d:%s",sender ,msg);
    std::list<int>::iterator it;
    for(it=connList.begin(); it!=connList.end(); ++it)
    {
        //给其他人发送消息
        if (sender != *it)
        {
            send(*it, buf, strlen(buf), 0);
        }
    }
}
 
void getConn()
{
    while(1)
    {
        int conn = accept(mSocket, (struct sockaddr*)&servaddr, &len);
        connList.push_back(conn);
        //printf("%d\n", conn);
        printf("玩家 %d 进入房间 \n",conn);
    }
}
 
void getData()
{
    struct timeval tv;
    tv.tv_sec = 10;//设置倒计时时间
    tv.tv_usec = 0;
    while(1)
    {
        std::list<int>::iterator it;
        for(it=connList.begin(); it!=connList.end(); ++it)
        {
            fd_set rfds;
            FD_ZERO(&rfds);
            int maxfd = 0;
            int retval = 0;
            FD_SET(*it, &rfds);
            if(maxfd < *it)
            {
                maxfd = *it;
            }
            retval = select(maxfd+1, &rfds, NULL, NULL, &tv);
            if(retval == -1)
            {
                printf("select error\n");
            }
            else if(retval == 0)
            {
                //printf("not message\n");
            }
            else
            {
                char buf[1024];
                memset(buf, 0 ,sizeof(buf));
                int len = recv(*it, buf, sizeof(buf), 0);
                
                if (len > 0) {
                    
                    
                    printf("收到 %d的消息 %s", *it ,buf);
                    
                    sendMsg(*it, buf, len);
                }
                
                
            }
        }
        //sleep(1);
 
    }
}
 
 
 
 
 
 
 
int main()
{
    //new socket
    std::cout<<"1.创建socket"<<std::endl;
    mSocket = socket(AF_INET, SOCK_STREAM, 0);
    
    
    memset(&servaddr, 0, sizeof(servaddr));
    
    servaddr.sin_family = AF_INET;
    
    //设置服务器监听的端口
    servaddr.sin_port = htons(PORT);
    
    
    //如果是局域网，这里可以使用IP127.0.0.1
    //servaddr.sin_addr.s_addr = inet_addr(IP);
    //如果部署到linux服务器 这里使用htonl(INADDR_ANY)
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    
    std::cout<<"2.绑定socket和端口号"<<std::endl;
    if(bind(mSocket, (struct sockaddr* ) &servaddr, sizeof(servaddr))==-1)
    {
        perror("bind");
        exit(1);
    }
    std::cout<<"3.监听端口号"<<std::endl;
    
    if(listen(mSocket, 20) == -1)
    {
        perror("listen");
        exit(1);
    }
    len = sizeof(servaddr);
 
    //thread : while ==>> accpet
    std::thread t(getConn);
    t.detach();//detach的话后面的线程不同等前面的进程完成后才能进行，如果这里改为join则前面的线程无法判断结束，就会
    //一直等待，导致后面的线程无法进行就无法实现操作
    //printf("done\n");
    
    
    //thread : recv ==>> show
    std::thread t2(getData);
    t2.detach();
    while(1)//做一个死循环使得主线程不会提前退出
    {
 
    }
    return 0;
}
